
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/md5_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/nim_sdk_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_contact/model/interest_chat_channel_entity.dart';
import 'package:module_business/module_contact/service/module_contact_service.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:nim_core/nim_core.dart';

class ContactViewModel extends BaseViewModel<ModuleContactService>{

  ModuleContactService moduleContactService;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RefreshState loadStatus=RefreshState.OK;
  int page = 1;
  bool isCanMoreData = false;
  InterestChatChannelEntity interestChatChannelEntity = InterestChatChannelEntity();

  Rx<NIMChatroomInfo> sportChatRoomInfo = NIMChatroomInfo(onlineUserCount: null).obs;
  Rx<NIMChatroomInfo> musicChatRoomInfo = NIMChatroomInfo(onlineUserCount: null).obs;
  Rx<NIMChatroomInfo> yogaChatRoomInfo = NIMChatroomInfo(onlineUserCount: null).obs;

  @override
  ModuleContactService createService() =>moduleContactService = ModuleContactService();

  onRefresh({int pageCur,}) async{
    Map<String,dynamic> upData={
      'searchValue': 'Sport,Music,Yoga',
    };
    BaseEntity baseEntity= await moduleContactService
        .api_expand_sigleParentChat_getChatRoomByKeys(parmsMap: upData);
    if(refreshController!=null)refreshController.refreshCompleted();
    if(refreshController!=null)refreshController.loadComplete();
    if(baseEntity?.code==ApiConfig.SUCCESS_CODE){
      interestChatChannelEntity = baseEntity?.result??InterestChatChannelEntity();
      //获取对应的云信聊天室
      // getChatRooms();
    }else{
      loadStatus=RefreshState.NET_ERROR;
      ToastUtil.toast(baseEntity?.message);
    }
    update();
  }

  void getChatRooms(){
    var sportChatRoomId = interestChatChannelEntity?.sport??'';
    if(sportChatRoomId.isNotEmpty)getSportChatRoomInfo(sportChatRoomId);
    var musicChatRoomId = interestChatChannelEntity?.music??'';
    if(musicChatRoomId.isNotEmpty)getMusicChatRoomInfo(musicChatRoomId);
    var yogaChatRoomId = interestChatChannelEntity?.yoga??'';
    if(yogaChatRoomId.isNotEmpty)getYogaChatRoomInfo(yogaChatRoomId);
  }

  void getSportChatRoomInfo(String sportChatRoomId) async{
    NIMResult<NIMChatroomInfo> roomInfoResult =
    await NIMSDKUtil.instance.fetchChatroomInfo(
      roomId: sportChatRoomId,
    );
    if(roomInfoResult?.isSuccess){
      sportChatRoomInfo.value = roomInfoResult?.data;
      LogUtil.d('sportChatRoomInfo--->${sportChatRoomInfo?.toJson()}');
    }
  }
  void getMusicChatRoomInfo(String musicChatRoomId) async{
    NIMResult<NIMChatroomInfo> roomInfoResult =
    await NIMSDKUtil.instance.fetchChatroomInfo(
      roomId: musicChatRoomId,
    );
    if(roomInfoResult?.isSuccess){
      musicChatRoomInfo.value = roomInfoResult?.data;
      LogUtil.d('musicChatRoomInfo--->${musicChatRoomInfo?.toJson()}');
    }
  }
  void getYogaChatRoomInfo(String yogaChatRoomId) async{
    NIMResult<NIMChatroomInfo> roomInfoResult =
    await NIMSDKUtil.instance.fetchChatroomInfo(
      roomId: yogaChatRoomId,
    );
    if(roomInfoResult?.isSuccess){
      yogaChatRoomInfo.value = roomInfoResult?.data;
      LogUtil.d('yogaChatRoomInfo--->${yogaChatRoomInfo?.toJson()}');
    }
  }

  chatItemTap(String type) {
    String roomId = '';
    if(type == StringUtil.instance.Sport){
      roomId = interestChatChannelEntity.sport;
    }else if(type == StringUtil.instance.Music){
      roomId = interestChatChannelEntity.music;
    }else if(type == StringUtil.instance.Yoga){
      roomId = interestChatChannelEntity.yoga;
    }
    LogUtil.d('chatItemTap,ChatRoomViewModel--->roomId=$roomId');
    if((roomId??'').isEmpty)return;
    // NavigatorUtil.pushByName(RouteConfig.chatRoomPage,arguments: {'roomId': roomId});
    toRoom(roomId,type);

  }
  void toRoom(yxRoomId,type) async{
    //api_voicechat_room_joinRoom
    LoadUtil.showLoad(loadingText: StringUtil.instance.data_loading);
    //云信进入房间
    var  enterChatroomResult = await  NimCore.instance.chatroomService.enterChatroom(
      NIMChatroomEnterRequest(
        roomId: yxRoomId,
        nickname: UserParamsModel.instance.selfNIMUser.nick,
        avatar: UserParamsModel.instance.selfNIMUser.avatar,
        retryCount: 3,
      ),
    );
    LoadUtil.hideLoad();
    var enterChatroomResultMap = enterChatroomResult?.toMap();
    var enterChatroomResultData = enterChatroomResult?.data;
    LogUtil.d('进入聊天室------>enterChatroomResultMap=${enterChatroomResultMap}');
    if (enterChatroomResult.isSuccess) {
      LogUtil.d('进入聊天室成功');
      NavigatorUtil.pushByName(RouteConfig.chatRoomPage,arguments: {'yxRoomId': yxRoomId,'fromType':type});
    }else{
      LogUtil.d('进入聊天室失败');
    }
  }
}
